package com.star.base.dao;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Set;

public class UpdateListPlusProvider extends MapperTemplate {

    public UpdateListPlusProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    /**
     * 批量通过主键更新不为null的字段
     * 要求：批量修改多条记录的字段及值都一样，如：批量修改更新人和更新时间
     * sql = update tableName set colum = #{columValue}  where id in ();
     *
     * @param ms
     * @return
     */
    public String updateBatchByIds(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        // update tableName
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));

        // set colum = #{columValue}
        boolean isEmpty = isNotEmpty();
        String entityName = "param1";
        sql.append("<set>");
        //获取全部列
        Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        //当某个列有主键策略时，不需要考虑他的属性是否为空，因为如果为空，一定会根据主键策略给他生成一个值
        for (EntityColumn column : columnList) {
            if (!column.isId() && column.isUpdatable()) {
                sql.append(SqlHelper.getIfNotNull(entityName, column, column.getColumnEqualsHolder(entityName) + ",", isEmpty));
            }
        }
        sql.append("</set>");
        // where id in ()
        sql.append("<where>");
        Set<EntityColumn> pkColumns = EntityHelper.getPKColumns(entityClass);
        for (EntityColumn column : pkColumns) {
            sql.append(" AND " + column.getColumn() + " in ");
            sql.append("<foreach collection=\"param2\" item=\"record\" separator=\",\" open=\"(\" close=\")\">");
            sql.append(" #{record} ");
            sql.append("</foreach>");
        }
        sql.append("</where>");
        return sql.toString();
    }

    /**
     * 批量更新
     * update tableName set column = #{columnValue1} where id = #{id1};
     * update tableName set column = #{columnValue2} where id = #{id2};
     *
     * @param ms
     * @return
     */
    public String updateBatchByPrimaryKeySelective(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();

        sql.append("<foreach collection=\"collection\" item=\"record\" separator=\";\" >");
        // update tableName
        boolean isEmpty = isNotEmpty();
        String entityName = "record";
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
        // set colum = #{columValue}
        sql.append("<set>");
        //获取全部列
        Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        //当某个列有主键策略时，不需要考虑他的属性是否为空，因为如果为空，一定会根据主键策略给他生成一个值
        for (EntityColumn column : columnList) {
            if (!column.isId() && column.isUpdatable()) {
                sql.append(SqlHelper.getIfNotNull(entityName, column, column.getColumnEqualsHolder(entityName) + ",", isEmpty));
            }
        }
        sql.append("</set>");
        // where id = #{id}
        sql.append("<where>");
        //获取全部列
        Set<EntityColumn> pkColumns = EntityHelper.getPKColumns(entityClass);
        //当某个列有主键策略时，不需要考虑他的属性是否为空，因为如果为空，一定会根据主键策略给他生成一个值
        for (EntityColumn column : pkColumns) {
            sql.append(" AND " + column.getColumnEqualsHolder(entityName));
        }
        sql.append("</where>");
        sql.append("</foreach>");

        return sql.toString();
    }

}
